<?php
class Upload extends Controller
{
	public function _construct()
	{
		fw::lang('upload');
	}
	
	public function uploader()
	{
		Template::script('jquery', 'uploader');
		
		$file_limit = fw::config('file_limit');
		$dimensions = explode('x', fw::config('dimensions'));
		
		Template::script_var('WEB', WEB);
		Template::script_var('IMG', WEB.'style/'.fw::config('style').'/image/img.png');
		Template::script_var('UPLOADED', WEB.'style/'.fw::config('style').'/image/uploaded.png');
		Template::script_var('LOADING', WEB.'style/'.fw::config('style').'/image/loading.gif');
		Template::script_var('UPLOAD_ID', uniqid());
		Template::script_var('FILE_LIMIT', (int) fw::config('file_limit'));
		Template::script_var('MAX_WIDTH', (int) $dimensions[0]);
		Template::script_var('MAX_HEIGHT', (int) $dimensions[1]);
		Template::script_var('UPLOAD_LIMIT', (int) fw::config('upload_limit'));
		Template::script_var('info', lang('notification', array('size'=>size($file_limit))));
		Template::script_var('error', array
		(
			lang('error_url'),
			lang('error_type'),
			lang('error_size', array('size'=>size($file_limit))),
			lang('error_amount', fw::config('upload_limit')),
			lang('error_dim', array('max'=>fw::config('dimensions')))
		));
		
		$recent = new Image('WHERE `private`=0');
		
		$total = number_format($recent->count(), 0, null, lang('thousand_separator'));
		
		$recent->query('ORDER BY `id` DESC LIMIT 12');
		
		require Template::render('Upload', 'uploader.html');
	}
	
	public function configure($id, $alpha=null)
	{
		$id = db::escape($id);
		
		if(db::num_rows(db::query('SELECT NULL FROM `'.DB_PREFIX.'upload` WHERE `id`="'.$id.'"')))
		{
			if(isset($_POST['configure']))
			{
				db::query('DELETE FROM `'.DB_PREFIX.'upload` WHERE `id`="'.$id.'"');
				
				$images = db::query('SELECT `id` FROM `'.DB_PREFIX.'Image` WHERE `upload_id`="'.$id.'"');
				while($row = db::fetch($images, 'row'))
				{
					db::query('UPDATE `'.DB_PREFIX.'Image` SET `views`=0, `date`=NOW(), `User_id`='.User::is_authenticated().', `private`='.(isset($_POST['private'.$row[0]])?1:0).', `title`="'.db::escape($_POST['title'.$row[0]]).'", `description`="'.db::escape($_POST['description'.$row[0]]).'" WHERE `id`='.$row[0]);
				}
			} else
			{
				$images = new Image('WHERE `upload_id`="'.$id.'"');
				
				require Template::render('Upload', 'configure.html');
				
				exit();
			}
		}
		
		fw::lang('gallery');
		
		// Get ID
		if($alpha)
		{
			$index = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ';
			
			$base = strlen($index);

			$in  = strrev($alpha);
			$out = 0;
			$len = strlen($in) - 1;

			for($t=0; $t<=$len; $t++)
			{
				$bcpow = bcpow($base, $len - $t);
				
				$out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
			}

			$out = sprintf('%F', $out);

			$img = substr($out, 0, strpos($out, '.'));
		} else
		{
			$row = db::fetch(db::query('SELECT `id` FROM `'.DB_PREFIX.'Image` WHERE `upload_id`="'.$id.'" ORDER BY `id` ASC LIMIT 1'), 'row');
			$img = $row[0];
		}
		
		// Image
		$image = new Image($img);
		
		if(!$alpha) $alpha = $image->alpha();
		
		$image->views++;
		
		// HTTP
		$http = 'http://'.$_SERVER['HTTP_HOST'].WEB;
		
		// Gallery
		$order = fw::get_cookie('order', 'new');
		$range = fw::get_cookie('range', 'all');
		$page = fw::get_cookie('page', 1);
		
		// Slider
		$index = 0;
		
		$query = 'WHERE `upload_id`="'.$id.'"';
		
		$gallery = new Image($query);
		$total = $gallery->count();
		
		// Order
		$query .= ' ORDER BY `id` ASC';
		
		// Get index
		$result = db::query('SELECT `id` FROM `'.DB_PREFIX.'Image` '.$query);
		while($row = db::fetch($result, 'row'))
		{
			if($row[0] == $image->id) break;
			
			$index++;
		}
		
		$galleries = array();
		
		$min = $index - 2;
		$max = $index + 2;
		
		if($total <= 5)
		{
			$galleries[] = new Image($query);
		} else
		{
			if($min < 0)
			{
				$galleries[] = new Image($query.' LIMIT '.($total + $min).',5');
				$galleries[] = new Image($query.' LIMIT 0,'.(5 + $min));
				
				$min = $total + $min;
			} else
			{
				$galleries[] = new Image($query.' LIMIT '.$min.',5');
				
				if($max >= $total)
				{
					$galleries[] = new Image($query.' LIMIT 0,'.($max - $total + 1));
				}
			}
			
			Template::script_var('INDEX', $min);
		}
		
		// Delete
		if(isset($_POST['delete']) && User::is_authenticated() && (User::is_authenticated() == $image->User_id || User::is_admin()))
		{
			db::query('DELETE FROM `'.DB_PREFIX.'Image` WHERE `id`='.$image->id);
			
			if($total == 1)
			{
				header('Location: '.WEB.'user/'.User::username().'/');
			} else
			{
				header('Location: '.WEB.$id.'/');
			}
			
			exit();
		}
		
		// Template
		Template::script('jquery', 'view');
		
		define('PATH', WEB.$id.'/');
		
		Template::script_var('WEB', WEB);
		Template::script_var('PATH', PATH);
		Template::script_var('PLACE', 0);
		Template::script_var('ORDER', null);
		Template::script_var('RANGE', $id);
		Template::script_var('CURRENT', $image->id);
		
		require Template::render('Gallery', 'view.html');
	}
}